import { createStorage } from '@/utils/storage'
const itemSessionStorage = createStorage({ storage: sessionStorage })
import { CURRENT_COURSE_PLAY_ITEM } from '@/store/mutation-types'
import util from '@/utils/test.js'
const state = {
  currentPlayItem: itemSessionStorage.get(CURRENT_COURSE_PLAY_ITEM, null), // 当前播放item
  currentPlayPosition: null, // 当前播放位置
  currentSectionPlayLock: [] //  播放时保存解锁的课时id
}

const mutations = {
  // 当前课时播放的item
  setCurrentPlayItem(state, data) {
    state.currentPlayItem = data
    itemSessionStorage.set(CURRENT_COURSE_PLAY_ITEM, data)
  },
  // 当前课程播放的定位
  currentPlayPosition(state, data) {
    state.currentPlayPosition = data
  },
  // 保存解锁的课时id，播放过后解除锁定状态
  setPlaySectionLock(state, id) {
    let list = state.currentSectionPlayLock
    if (!list.includes(id)) {
      state.currentSectionPlayLock.push(id)
    }
  }
}

const actions = {
  setCurrentPlayPosition({ commit }, data) {
    commit('currentPlayPosition', data)
  }
}

const getters = {
  // 班级课当前播放课程id
  getCurrentPlayCourseid(state) {
    return util.isDef(state.currentPlayPosition) ? state.currentPlayPosition.course_id : 0
  },
  // 当前播放定位
  getCurrentPlayPosition(state) {
    return util.isDef(state.currentPlayPosition) ? state.currentPlayPosition.position : []
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}
